package com.wangyang.stock.business.services;

import java.util.ArrayList;
import java.util.List;

import com.wangyang.stock.business.IMerInvService;
import com.wangyang.stock.dao.IDao;
import com.wangyang.stock.po.MerInv;
import com.wangyang.stock.vo.MerInvVo;

public class MerInvServiceImp implements IMerInvService {
	private IDao dao;

	public IDao getDao() {
		return dao;
	}

	public void setDao(IDao dao) {
		this.dao = dao;
	}
/**
 * 此方法根据单据类别与单据时间来对货品的流量进行统计
 */
	public List selectMerInv(int typeId, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer("select mer3.merchandise_name as name , mer2.amount from merchandise mer3 , ( select mi.merchandise_id , sum(  mi.merchandise_amount ) as amount from mer_inv mi , ( select inv.invoice_id from invoice inv where inv.invoice_type_id = "); 
		sql.append(typeId);	 
		sql.append(" and inv.invoice_date BETWEEN '");	
		sql.append(beginTime);	
		sql.append("' AND '");	
		sql.append(endTime);
		sql.append("' ) inv2  where mi.invoice_id = inv2.invoice_id group by mi.merchandise_id ) mer2 where mer3.merchandise_id = mer2.merchandise_id");
//		System.out.println(sql);
		return dao.selectBySQL(sql.toString());
	}

	public List selectMerInvByInvId(int invoiceId) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from MerInv mi where mi.invoice.invoiceId=");
		hql.append(invoiceId);
		List list = dao.select(hql.toString());
		List volist = new ArrayList();
		for (int i = 0; i < list.size(); i++) {
			MerInv mi = (MerInv)list.get(i);
			MerInvVo miVo = new MerInvVo();
			miVo.setMerchandiseName(mi.getMerchandise().getMerchandiseName());
			miVo.setMerchandiseAmount(mi.getMerchandiseAmount());
			miVo.setMerchandisePrice(mi.getMerchandisePrice());
			miVo.setMerchandiseTotal(mi.getMerchandiseTotal());
			volist.add(miVo);
		}
		return volist;
	}
}
